#include <iostream>
#include <vector>
#include <stack>
using namespace std;

vector<int> signal_tower(vector<int>&heights,vector<int>&out_put)
{
    int n = heights.size();
    vector<int>results(n,0);
    if (n <=1 )                 return results;
    if (n != out_put.size())    return results;

    stack<int>s;
    vector<int>left(n,0);
    vector<int>right(n,0);

    while (!s.empty())          s.pop();
    for (int i = 0;i < n;i++)
    {
        while (!s.empty()&&heights[s.top()] < heights[i])   s.pop();
        left[i] = s.empty()? -1:s.top();
        s.push(i);
    }

    while (!s.empty())          s.pop();
    for (int i = n-1;i >= 0;i--)
    {
        while (!s.empty()&&heights[s.top()]<heights[i])     s.pop();
        right[i] = s.empty()? n:s.top();
        s.push(i);
    }

    for (int i = 0;i < n;i++)
    {
        if (left[i] >= 0)   results[left[i]] += out_put[i];
        if (right[i] != n)  results[right[i]] += out_put[i];
    }

    return results;
}

int main()
{
    vector<int>heights = {2,3,5,2,1,4};
    vector<int>out_put = {1,0,1,9,1,2};
    vector<int>res = signal_tower(heights,out_put);
    for (int i:res)
    {
        cout<<i<<" ";
    }
    return 0;
}